<template>
  <!-- 引入container布局 -->
  <el-container class="home_container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img
          src="../assets/shanghai.jpeg"
          alt=""
          style="width: 50px; height: 40px"
        />
      </div>
      <span style="font-family: '幼圆'"
        >宁乡办公区员工工作日随机“抽奖”后台管理</span
      >
      <el-button type="info" @click="logout">安全退出</el-button>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle_button" @click="toggleCollapase">|||</div>
        <el-menu
          background-color=" #333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            v-for="item in menuList"
            :key="item.id"
            :index="item.id + ''"
          >
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{ item.title }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              v-for="i in item.sList"
              :key="i.id"
              :index="i.path"
              @click="saveNavState(i.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ i.title }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      //菜单列表
      menuList: [],
      //伸缩
      isCollapse: false,
      activePath: "/welcome", //默认路径
      luckPerson: "", //用来暂存被抽中的幸运儿的名字
    };
  },
  //onload事件
  created() {
    //查询menulist
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath"); //
  },
  methods: {
    //退出事件
    logout() {
      window.sessionStorage.clear(); //清除session(用户信息)
      this.$router.push("/login"); //回到首页
    },
    //获取导航菜单方法
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.flag != 200) return this.$message.error("获取列表失败！！！"); //获取失败
      this.menuList = res.menus; //获取成功则将表单填入menuList
    },

    //控制伸缩
    toggleCollapase() {
      this.isCollapse = !this.isCollapse;
    },
    //保存路径
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    },
  },
};
</script>

<style lang="less" scoped>
//布局器
.home_container {
  height: 100%;
}

//头部
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between; //左右贴边
  padding-left: 0%; //左边界
  color: #fff;
  font-size: 20px;
  align-items: center;
}
.el-header span {
  margin-left: 15px;
}

//侧边栏
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
//主体
.el-main {
  background-color: #eaedf1;
}

img {
  width: 55px;
  height: 55px;
}

.toggle_button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 30px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em; //字体间距
  cursor: pointer; //显示小手
}
</style>